package com.gmscosmo.janus.handler;

import com.alibaba.fastjson.JSONObject;
import com.gmscosmo.janus.util.JanusLogUtil;
import com.gmscosmo.model.*;
import com.gmscosmo.request.*;
import com.gmscosmo.janus.util.CommonUtils;
import com.gmscosmo.common.JanusNativeSignaling;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassName: JanusStreamingRabbitMqEventHandler
 * Description:
 * Date: 2021/10/12 10:54
 *
 * @author stephen.qiu
 * @version 1.0
 */
@Component
public class JanusStreamingRabbitMqEventHandler extends JanusStreamingHandler {

    /**
     * 创建流接受
     * @param session_id
     * @param handle_id
     * @param display
     * @param streamId
     * @param rtpForwardData
     * @param userId
     * @param nodeId
     * @return
     * @throws Exception
     */
    @Override
    public String createStream(long session_id, long handle_id, String display, int streamId, RtpForwardData rtpForwardData, String userId, int nodeId) throws Exception {
        JanusRequest createStreamRequest = new JanusRequest();
        createStreamRequest.setJanus(JanusNativeSignaling.MESSAGE);
        String transaction = CommonUtils.randomString(12);
        createStreamRequest.setTransaction(transaction);
        createStreamRequest.setSession_id(session_id);
        createStreamRequest.setHandle_id(handle_id);

        CreateStreamBody createStreamBody = new CreateStreamBody();
        createStreamBody.setRequest(JanusNativeSignaling.CREATE);
        createStreamBody.setType("rtp");
        createStreamBody.setId(streamId);
        createStreamBody.setAudio(true);
        createStreamBody.setVideo(true);
        createStreamBody.setDescription(userId + "|" + streamId);
        //TODO
        createStreamBody.setAudioport(rtpForwardData.getAudio_port());
        createStreamBody.setVideoport(rtpForwardData.getVideo_port());
        createStreamBody.setVideoport2(rtpForwardData.getVideo_port_2());
        createStreamBody.setVideoport3(rtpForwardData.getVideo_port_3());
        createStreamBody.setVideortcpport(rtpForwardData.getVideo_rtcp_port());
        createStreamBody.setVideosimulcast(true);
        createStreamRequest.setBody(createStreamBody);
        JSONObject response = janusAsyncSender.sendWithFuture(transaction, false, JSONObject.toJSON(createStreamRequest).toString(), nodeId).get();
        addSubcriber(session_id, handle_id, display, streamId,rtpForwardData, userId);
        return response.toString();
    }

    @Override
    public void destroyMyStream(String userId) throws Exception {
        if (!janusUtil.isSubscribersContains(userId)) {
            janusLogUtil.log("no stream had to destroy.",this.getClass(),JanusLogUtil.INFO);
            return;
        }
        JanusRequest destroyStreamRequest = new JanusRequest();
        destroyStreamRequest.setJanus(JanusNativeSignaling.MESSAGE);
        String transaction = CommonUtils.randomString(12);
        destroyStreamRequest.setTransaction(transaction);
        DestroyStreamBody destroyStreamBody = new DestroyStreamBody();
        List<Subscriber> subscriberList = janusUtil.getSubscribers(userId);
        for (Subscriber subscriber : subscriberList) {
            destroyStreamRequest.setSession_id(subscriber.getSession_id());
            janusUtil.removePort(subscriber.getAudio_port());
            janusUtil.removePort(subscriber.getVideo_port());
            janusUtil.removePort(subscriber.getVideo_port_2());
            janusUtil.removePort(subscriber.getVideo_port_3());
            if (subscriber.getVideo_rtcp_port() != null) {
                janusUtil.removePort(subscriber.getVideo_rtcp_port());
            }
            destroyStreamBody.setId(subscriber.getId());
            destroyStreamRequest.setHandle_id(subscriber.getHandle_id());
            destroyStreamRequest.setBody(destroyStreamBody);
            int nodeId = janusUtil.getUserNode(userId);
            janusAsyncSender.sendWithFuture(transaction, false, JSONObject.toJSON(destroyStreamRequest).toString(), nodeId).get();
        }
        janusUtil.removeSubscribers(userId);
    }

    /**
     * 选择观看流
     *
     * @param session_id
     * @param handle_id
     * @param id
     * @param nodeId
     * @return
     * @throws Exception
     */
    @Override
    public String watch(long session_id, long handle_id, Integer id, int nodeId) throws Exception {
        JanusRequest watchRequest = new JanusRequest();
        watchRequest.setJanus(JanusNativeSignaling.MESSAGE);
        String transaction = CommonUtils.randomString(12);
        watchRequest.setTransaction(transaction);
        watchRequest.setSession_id(session_id);
        watchRequest.setHandle_id(handle_id);

        WatchBody watchBody = new WatchBody();
        watchBody.setRequest(JanusNativeSignaling.WATCH);
        watchBody.setId(id);
        watchRequest.setBody(watchBody);
        JSONObject response = janusAsyncSender.sendWithFuture(transaction, false, JSONObject.toJSON(watchRequest).toString(), nodeId).get();
        response.put("handle_id", handle_id);
        return response.toString();
    }
}
